package com.itlong.cloud.config;

import org.apache.coyote.ProtocolHandler;
import org.apache.coyote.http11.AbstractHttp11Protocol;
import org.springframework.boot.web.embedded.tomcat.ConfigurableTomcatWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Configuration;


/**
 * <desc>
 *      自定义tomcat容器参数调优。
 * </desc>
 *
 * @createDate 2020/11/25
 */
@Configuration
public class WebServerConfiguration implements WebServerFactoryCustomizer<ConfigurableTomcatWebServerFactory> {

    @Override
    public void customize(ConfigurableTomcatWebServerFactory factory){
        factory.addConnectorCustomizers((connector) -> {
            ProtocolHandler handler = connector.getProtocolHandler();
            if (handler instanceof AbstractHttp11Protocol) {
                AbstractHttp11Protocol<?> protocol = (AbstractHttp11Protocol) handler;
                //protocol.setKeepAliveTimeout(100);
                protocol.setMaxKeepAliveRequests(1);
            }
        });
    }
}
